<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\Article;
use think\Controller;
use think\Db;
use think\Request;

class Often extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function article()
    {
        //
        return $this->fetch('often/article');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function getArtcicleData()
    {
        $data=(new \app\admin\model\Article())->searchAll();
        return ['code'=>0,'msg'=>'success','data'=>$data,'count'=>"10"];

        //
    }
    /**
     * 添加文章
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
        //加载新增页面
    public  function addAt(){
        return view('often/article-add');
    }
    //添加缩略图
    public function upload()
    {
        $img = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $img->move(ROOT_PATH . 'public' . DS    . 'thumb_uploads');
        if($info){
            // 成功上传后 获取上传信息
            return json(['code' => 0, 'msg' => '上传成功!', 'url' => '/public/thumb_uploads/' . $info->getSaveName()]);
        }else{
            // 上传失败获取错误信息
            return json(['code' => 1, 'msg' => $img->getError(), 'url' => '']);
        }

    }

        //编辑器图片上传
    public function editUploads()
    {
        $file = request()->file('file');

        // 移动到框架应用根目录/public/uploads/ 目录下
        $data = [];
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'editor');
            if ($info) {
                $imgp = str_replace('\\','/',$info->getSaveName());

                $data = [
                    'errno' => 0,
                    'data' => !empty($info) ? ['/public/uploads/editor/'.$imgp] : []
                ];

            } else {
                $data = [
                    'errno' => $file->getError(),
                    'data' => []
                ];

            }
        }

        return json_encode($data);
    }


    //执行添加文章
    public function addArticle(Request $request)
    {

        return $dat=(new Article())->addArticle(input('post.'));
    }

    /**
     * 编辑文章
     *
     * @param  int  $id
     * @return \think\Response
     */

    public function readAt()
    {
       $data=(new Article())->searchId(\request()->param());
       $this->assign('data',$data);
       return view('often/article-edit');
    }
    public function updateArticle(){
    return (new Article())->updateArticle(\request()->param());
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }
    public function banner(){
    return view('often/image');
    }
    public function addBnner(){
        return view('often/image-add');
    }
    public function bannerUpload()
    {
        $img = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $img->move(ROOT_PATH . 'public' . DS . 'banner_uploads');
        if($info){
            // 成功上传后 获取上传信息
            return json(['code' => 0, 'msg' => '上传成功!', 'url' => '/public/banner_uploads/' . $info->getSaveName()]);
        }else{
            // 上传失败获取错误信息
            return json(['code' => 1, 'msg' => $img->getError(), 'url' => '']);
        }

    }
    //插入数据库
    public function adIm(){
     $data=$this->request->param();
     if (empty($data['status'])){
         $data['status']=0;
     }

     $res=Db::name('banner')->insert(
        [
            'ba_name'=>$data['title'],
            'status'=>$data['status'],
            'ba_src'=>$data['imagesrc'],
            'ba_src'=>$data['imagesrc'],
            'ba_admin'=>session('admin.admin_username'),
            'createTime'=>time()
            ]
     );

        return ['code'=>0,'msg'=>'success'];
    }
    //查询banner列表
    public function listImage(){
        $data=Db::name('banner')->select();
        return ['code'=>0,'msg'=>'success','data'=>$data];
    }
    public function batchDel(){
        $data=(new Article())->Delbanner(\request()->param());
        return['code'=>0,"msg"=>"success"];
    }
    public function batchDel1(){
        $data=(new Article())->delArticle(\request()->param());
        return['code'=>0,"msg"=>"success"];
    }

    public function selectServer(){
        $list = Db::name('cate')->select();
        return['code'=>0,'data'=>$list];
    }

}
